﻿using Loli.Library.Util;
using Loli.Library.WinIo;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace PeopleSimulator
{
    public delegate void UpdateInterface();


    public partial class MainForm : Form
    {
        //  private Loli.Library.WinIo.Action _Action = null;
        private List<Script> _Scripts = null;
        private string _DataFileName = "script.dat";
        private CreateScriptForm _CreateScriptForm = null;
        private RunForm _RunForm = null;

        public MainForm()
        {
            InitializeComponent();
            this.Text += " v" + Assembly.GetExecutingAssembly().GetName().Version;

            this.lvScripts.CheckBoxes = true;
            this.lvScripts.MultiSelect = false;
            /// this.lvScripts.CheckBoxes
            //this.lvScripts.DataBindings.Add(
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            this.cbTopMost.Checked = true;
            try
            {
                if (File.Exists(this._DataFileName))
                {
                    using (FileStream stream = File.Open(this._DataFileName, FileMode.Open, FileAccess.Read, FileShare.None))
                    {
                        BinaryFormatter format = new BinaryFormatter();
                        this._Scripts = (List<Script>)format.Deserialize(stream);
                        stream.Close();
                    }
                }
                else
                {
                    this._Scripts = new List<Script>();
                }
            }
            catch
            {
                this._Scripts = new List<Script>();
            }

            foreach (var srcipt in this._Scripts)
            {
                ListViewItem item = new ListViewItem() { Text = srcipt.Name, Name = srcipt.Name, Tag = srcipt };
                this.lvScripts.Items.Add(item);
            }

        }
        protected override void OnClosing(CancelEventArgs e)
        {
            base.OnClosing(e);
            //保存数据

            using (FileStream stream = File.Open(this._DataFileName, FileMode.Create, FileAccess.Write, FileShare.None))
            {
                BinaryFormatter format = new BinaryFormatter();
                format.Serialize(stream, this._Scripts);
                stream.Close();
            }

        }


        private void butRun_Click(object sender, EventArgs e)
        {
            if (this._RunForm == null)
            {
                this._RunForm = new RunForm();
            }


            ListView.CheckedListViewItemCollection list = this.lvScripts.CheckedItems;

            if (list == null || list.Count <= 0)
            {
                MessageBox.Show(this, "还没有按键数据，请先新增！", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                butScriptAdd.Focus();
                return;
            }

            // this.DisabledControls(true);

            ListViewItem item = this.lvScripts.CheckedItems[0];



            _RunForm.Script = item.Tag as Script;
            _RunForm.TopMost = cbTopMost.Checked;
            _RunForm.ShowDialog(this);

        }
        /*
        private void cbTopMost_CheckedChanged(object sender, EventArgs e)
        {
            this.TopMost = cbTopMost.Checked;
        }
        */


        private void butScriptAdd_Click(object sender, EventArgs e)
        {
            if (this._CreateScriptForm == null)
            {
                this._CreateScriptForm = new CreateScriptForm();
            }

            this._CreateScriptForm.Script = null;

            DialogResult result = this._CreateScriptForm.ShowDialog(this);

            if (result == System.Windows.Forms.DialogResult.OK)
            {
                Script srcipt = this._CreateScriptForm.Script;
                ListViewItem item = new ListViewItem() { Text = srcipt.Name, Name = srcipt.Name, Tag = srcipt };
                this.lvScripts.Items.Add(item);
                this._Scripts.Add(srcipt);
            }
        }

        private void lvScripts_ItemActivate(object sender, EventArgs e)
        {
            if (this._CreateScriptForm == null)
            {
                this._CreateScriptForm = new CreateScriptForm();
            }

            ListViewItem item = this.lvScripts.SelectedItems[0];
            this._CreateScriptForm.Script = item.Tag as Script;
            DialogResult result = this._CreateScriptForm.ShowDialog(this);
            if (result == System.Windows.Forms.DialogResult.OK)
            {
                Script srcipt = this._CreateScriptForm.Script;
                item.Text = srcipt.Name;
                item.Name = srcipt.Name;
                item.Tag = srcipt;
            }
        }

        private void lvScripts_ItemCheck(object sender, ItemCheckEventArgs e)
        {
            foreach (int item in this.lvScripts.CheckedIndices)
            {
                if (item != e.Index)
                    this.lvScripts.Items[item].Checked = false;
            }

        }

        private void butRemove_Click(object sender, EventArgs e)
        {
            if (this.lvScripts.SelectedItems != null && this.lvScripts.SelectedItems.Count > 0)
            {
                ListViewItem item = this.lvScripts.SelectedItems[0];
                Script script = item.Tag as Script;

                this.lvScripts.Items.Remove(item);
                this._Scripts.Remove(script);
            }
        }
    }
}
